import java.util.Stack;

//最小栈
public class MinStack {
    Stack<Integer> stack;
    Stack<Integer> minstack;
    //初始化
    public MinStack() {
        stack=new Stack<>();
        minstack=new Stack<>();
    }
    //入栈
    public void push(int val) {
        stack.push(val);
        if (minstack.isEmpty()){
            minstack.push(val);
        }else {
            //最小栈不为空
            if (val<=minstack.peek()){//如果要入stack栈的元素小于minstack.peek(),val入栈
                minstack.push(val);
            }
        }
    }
    //顶部元素出栈
    public void pop() {
        if (!stack.isEmpty()){
            Integer val=stack.pop();
            if (val.equals(minstack.peek())){
                minstack.pop();
            }
        }
    }
    //获取顶部元素
    public int top() {
        if (!stack.empty()){
            return stack.peek();
        }
        return -1;
    }
    //获取栈中最小元素
    public int getMin() {
        return minstack.peek();
    }
}
